using System;
using System.Reflection;
using System.Security;
using System.Windows.Forms;
using DZTC.UselessAppEver.Core;
using DZTC.UselessAppEver.UI;
using Microsoft.Win32;

namespace DZTC.UselessAppEver
{
    public static class Installer
    {
        private static string GetCommand(string appendix)
        {
            var module = Assembly.GetExecutingAssembly().Location;
            return String.Format("\"{0}\" {1}", module, appendix);
        }

        public static void UninstallApplication()
        {
            try
            {
                // argument exception occurs if key is not present

                try
                {
                    Registry.ClassesRoot
                        .OpenSubKey(Const.ANY_FILE, true)
                        .OpenSubKey("shell", true)
                        .DeleteSubKeyTree(Const.PROG_ID);
                }
                catch (ArgumentException ex)
                {
                    // supress
                }

                try
                {
                    Registry.ClassesRoot.DeleteSubKeyTree(Const.FILE_EXTENSION);
                }
                catch (ArgumentException ex)
                {
                    // supress
                }

                try
                {
                    Registry.ClassesRoot.DeleteSubKeyTree(Const.PROG_ID);
                }
                catch (ArgumentException ex)
                {
                    // supress
                }
            }
            catch (UnauthorizedAccessException)
            {
                Dialog.UninstallationFailed(Localization.TryAdminAccount);
            }
            catch (SecurityException)
            {
                Dialog.UninstallationFailed(Localization.TryAdminAccount);
            }
            catch (Exception ex)
            {
                Dialog.UninstallationFailed(ex.Message);
            }
        }

        public static void InstallApplication()
        {
            //try
            //{
                var encodeTo = Registry.ClassesRoot.OpenSubKey(Const.ANY_FILE, true)
                    .OpenSubKey("shell", true)
                    .CreateSubKey(Const.PROG_ID);

                encodeTo.SetValue(null, Localization.ShellEncodeToBase32);
                encodeTo.CreateSubKey("command").SetValue("", GetCommand("--encode %1"));

                var progId = Registry.ClassesRoot.CreateSubKey(Const.PROG_ID);

                progId.CreateSubKey("DefaultIcon")
                    .SetValue("", GetCommand(",0"));

                progId.CreateSubKey("shell")
                    .CreateSubKey("open")
                    .CreateSubKey("command")
                    .SetValue("", GetCommand("--decode %1"));


                Registry.ClassesRoot.CreateSubKey(Const.FILE_EXTENSION)
                    .SetValue("", Const.PROG_ID);
            //}
            //catch (UnauthorizedAccessException)
            //{
            //    Dialog.InstallationFailed(Localization.TryAdminAccount);
            //}
            //catch (SecurityException)
            //{
            //    Dialog.InstallationFailed(Localization.TryAdminAccount);
            //}
            //catch (Exception ex)
            //{
            //    Dialog.InstallationFailed(ex.Message);
            //}
        }
    }
}